package cn.java75.school;

import cn.java75.school.dao.TravelMapper;
import cn.java75.school.model.Travel;
import cn.school.util.MyBatisUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import org.apache.ibatis.session.SqlSession;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Map;

/**
 * @author oldliu
 * @since 1.0
 */
public class EtlData {
    public static void main(String[] args) {
        SqlSession s = MyBatisUtil.get(true);
        TravelMapper mapper = s.getMapper(TravelMapper.class);
        try {
            BufferedReader br = new BufferedReader(new FileReader("E:\\java_workspace\\temp\\SZT-bigdata\\.file\\2018record3.jsons"), 30 * 1024 * 1024);

            String str = null;
            int count=0;
            while ((str = br.readLine()) != null) {
                //JSON.parseObject(str,new TypeReference<List<Travel>>(){});
                JSONObject jsonObject = JSON.parseObject(str);
                List<JSONObject> data = (List<JSONObject>) jsonObject.get("data");
                //System.out.println(data.size());
                for (JSONObject jo : data) {
                    Travel travel = new Travel();
                    travel.setCardNo(jo.getString("card_no"));
                    travel.setCarNo(jo.getString("car_no"));
                    travel.setCloseDate(jo.getTimestamp("close_date"));
                    travel.setCompanyName(jo.getString("company_name"));
                    travel.setConnMark(jo.getString("conn_mark"));
                    travel.setDealDate(jo.getTimestamp("deal_date"));
                    travel.setDealType(jo.getString("deal_type"));
                    travel.setStation(jo.getString("station"));
                    travel.setEquNo(jo.getString("equ_no"));
                    travel.setDealValue(jo.getIntValue("deal_value"));
                    travel.setDealMoney(jo.getIntValue("deal_money"));
                    //System.out.println(travel);
                    mapper.insert(travel);
                    count++;
                }
            }
            br.close();
            System.out.println(count);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
